<!--  -->
<template>
  <div class=''>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="25%"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          :label="projectBuildingId==2?'楼层':'位置'"
          prop="projectFloorNumber"
        >
          <el-input
            v-model="ruleForm.projectFloorNumber"
            :placeholder="projectBuildingId==2?'请输入楼层':'请输入位置'"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="面积(㎡)"
          prop="area"
        >
          <el-input
            v-model="ruleForm.area"
            placeholder="请输入面积"
            @input.native="ruleForm.area = test(ruleForm.area)"
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="merterName"
          prop="meterName"
        >
          <el-input
            v-model="ruleForm.meterName"
            :placeholder="'请输入'+merterName"
          ></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          @click="handleClose"
          icon='el-icon-circle-close'
        >取 消</el-button>
        <el-button
          type="primary"
          icon='el-icon-circle-check'
          @click="doSubmit"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { mrpublicmeterrelate } from '@/api/market/meter/setting/person'
export default {
  name: 'publicAdd',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data () {
    // 这里存放数据
    return {
      title: '添加楼层电表',
      dialogVisible: false,
      ruleForm: {
        meterName: '',
        area: '',
        projectFloorNumber: ''
      },
      rules: {
        meterName: [{ required: true, message: this.type == 1 ? '请输入电表名称' : '请输入水表名称', trigger: 'blur' }],
        area: [{ required: true, message: '面积不能为空', trigger: 'blur' }],
        projectFloorNumber: [{ required: true, message: this.projectBuildingId == 'complex_building' ? '请输入楼层' : '请输入位置', trigger: 'blur' }]
      },
      method: '',
      projectBuildingId: '',
      type: '',
      id: '',
      merterName: '电表名称'
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    test (value) {
      value = value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')
      return value
    },
    doSubmit () {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.method == 'add' || this.method == 'edit') {
            if (this.method == 'add') {
              this.ruleForm.publicMeterId = this.id
            }
            mrpublicmeterrelate('post', this.ruleForm).then(res => {
              this.handleClose()
              if (this.method == 'add') {
                this.$message.success("添加成功")
              } else {
                this.$message.success("修改成功")
              }
              this.$emit("refreshList")
            })
          }
        } else {
          return false
        }
      })
    },
    handleClose () {
      if (this.$refs.ruleForm) {
        this.$refs.ruleForm.resetFields()
      }
      this.dialogVisible = false
    },
    init (method, type, projectBuildingId, id) {
      this.method = method
      this.type = type
      this.projectBuildingId = projectBuildingId
      this.id = id
      this.ruleForm = {
        meterName: '',
        area: '',
        projectFloorNumber: ''
      }
      if (method == 'add') {
        if (type == 1) {
          this.title = "添加楼层电表"
          this.merterName = '电表名称'
        } else {
          this.title = "添加楼层水表"
          this.merterName = '水表名称'
        }
      } else if (method == 'edit') {
        this.ruleForm = JSON.parse(JSON.stringify(id))
        if (type == 1) {
          this.title = "修改楼层电表"
          this.merterName = '电表名称'
        } else {
          this.title = "修改楼层水表"
          this.merterName = '水表名称'
        }
      }
      this.dialogVisible = true
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）优惠卷
  created () {

  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {

  },
  beforeCreate () { }, // 生命周期 - 创建之前
  beforeMount () { }, // 生命周期 - 挂载之前
  beforeUpdate () { }, // 生命周期 - 更新之前
  updated () { }, // 生命周期 - 更新之后
  beforeDestroy () { }, // 生命周期 - 销毁之前
  destroyed () { }, // 生命周期 - 销毁完成
  activated () { } // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css类
</style>